﻿using System;
using System.Collections.Generic;
using System.Linq;
using AzureConnection.Models;

namespace AzureConnection
{
    public class Repository
    {
        public bool AddChild()
        {
            var success = false;
            var child = new Child
            {
                CPR = "120511-5678",
                Name = "Viggo Jensen"
            };
            
            using (var ctx = new PainRatingEntities())
            {
                var dbChild = ctx.Child.FirstOrDefault(x => x.CPR.Equals(child.CPR));
                if (dbChild == null)
                {
                    ctx.Child.Add(child);
                    ctx.SaveChanges();
                    success = true;
                }
            }

            return success;
        }

        public ChildModel GetChild(string cpr)
        {
            ChildModel child = null;

            using (var ctx = new PainRatingEntities())
            {
                var dbChild = ctx.Child.First(x => x.CPR.Equals(cpr));
                if (dbChild != null)
                {
                    child = new ChildModel
                    {
                        ID = dbChild.ID,
                        CPR = dbChild.CPR,
                        Name = dbChild.Name,
                        PainScoring = new List<PainScoringModel>()
                    };

                    if (dbChild.PainScoring != null)
                    {
                        var tmpLst = new List<PainScoringModel>();
                        foreach (var ps in dbChild.PainScoring)
                        {
                            tmpLst.Add(new PainScoringModel
                            {
                                ID = ps.ID,
                                Date = ps.Date,
                                Note = ps.Note,
                                PainRating = ps.PainRating
                            });
                        }

                        child.PainScoring = tmpLst;
                    }
                }
            }

            return child;
        }

        public IEnumerable<ChildModel> GetChildren()
        {
            var res = new List<ChildModel>();

            using (var ctx = new PainRatingEntities())
            {
                var query = ctx.Child.ToList();
                foreach (var child in query)
                {
                    var newChild = new ChildModel
                    {
                        ID = child.ID,
                        CPR = child.CPR,
                        Name = child.Name,
                        PainScoring = new List<PainScoringModel>()
                    };

                    if (child.PainScoring != null)
                    {
                        var tmpLst = new List<PainScoringModel>();
                        foreach (var ps in child.PainScoring)
                        {
                            tmpLst.Add(new PainScoringModel
                            {
                                ID = ps.ID,
                                Date = ps.Date,
                                Note = ps.Note,
                                PainRating = ps.PainRating
                            });
                        }

                        newChild.PainScoring = tmpLst;
                    }

                    res.Add(newChild);
                }
            }

            return res;
        }

        public ChildModel ValidateCPR(string cpr)
        {
            ChildModel child = null;

            using (var ctx = new PainRatingEntities())
            {
                var dbChild = ctx.Child.FirstOrDefault(x => x.CPR.Equals(cpr));
                if (dbChild != null)
                {
                    child = new ChildModel
                    {
                        ID = dbChild.ID,
                        CPR = dbChild.CPR,
                        Name = dbChild.Name
                    };
                }
            }

            return child;
        }

        public bool SetPainScore(string cpr, int score, string note)
        {
            using (var ctx = new PainRatingEntities())
            {
                var dbChild = ctx.Child.FirstOrDefault(x => x.CPR.Equals(cpr));
                if (dbChild != null)
                {
                    dbChild.PainScoring.Add(new PainScoring
                    {
                        PainRating = score,
                        Note = note,
                        Date = DateTime.Now
                    });
                    ctx.SaveChanges();
                }
            }

            return true;
        }
    }
}
